# A configurable script that unarchives TARGET and all LIBRARIES
# and then merges them back into a single TARGET using the proper ar tool

# Setup
set (TARGETLOC "@TARGETLOC@")
set (LIBRARIES @LIBRARIES@) # Do NOT quote libraries since we need to loop over the list
set (CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@")
set (CMAKE_AR "@CMAKE_AR@")
set (CMAKE_RANLIB "@CMAKE_RANLIB@")
unset (AROBJS)
get_filename_component (TARGETNAME ${TARGETLOC} NAME) 
set (ARDIR ${CMAKE_CURRENT_BINARY_DIR}/PosixMergeStaticLibraries-${TARGETNAME})
make_directory (${ARDIR})

# Extract every dependent library into a series of object files
foreach (LIB ${LIBRARIES})
	get_filename_component (LIBEXT ${LIB} EXT)
	if (${LIBEXT} MATCHES "^.so.*")
		message(STATUS "Not merging library ${LIB}")
	else (${LIBEXT} MATCHES "^.so.*")	
		get_filename_component (LIBNAME ${LIB} NAME)
		file (MAKE_DIRECTORY ${ARDIR}/${LIBNAME})
		execute_process (
			COMMAND ${CMAKE_AR} x ${LIB} WORKING_DIRECTORY ${ARDIR}/${LIBNAME}
		)
		file (GLOB_RECURSE OBJS "${ARDIR}/${LIBNAME}/*")
		set (AROBJS ${AROBJS} ${OBJS})
	endif (${LIBEXT} MATCHES "^.so.*")
endforeach (LIB)

# Merge all extracted object files into the existing target archive
execute_process (
	COMMAND ${CMAKE_AR} r ${TARGETLOC} ${AROBJS} WORKING_DIRECTORY ${ARDIR}
)

# Ranlib the new archive library
execute_process (
	COMMAND ${CMAKE_RANLIB} ${TARGETLOC} WORKING_DIRECTORY ${ARDIR}
)

# Delete all leftover object files
file (REMOVE_RECURSE ${ARDIR})
